//
//  ClassDataViewController.m
//  Feel_at_home
//
//  Created by dlios on 15/9/25.
//  Copyright © 2015年 Feel_at_home. All rights reserved.
//

#import "ClassDataViewController.h"
#import "Constants.h"
#import <UIImageView+WebCache.h>
#import "HTTPTOOL.h"
#import "WidthAndHeight.pch"
#import "ClassDataVewCell.h"
#import "ClassModel.h"
#import "HomeDataViewController.h"
@interface ClassDataViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *modelArr;

@end

@implementation ClassDataViewController

// 初始化
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.modelArr = [NSMutableArray array];
        
    }
    return self;
}

// 网络解析
- (void)didHeandle

{
    
    NSString *strUrl = [NSString stringWithFormat:@ClassDataUrl, self.ID];
    
    [HTTPTOOL GETWithURL:strUrl body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        
        NSArray *arr = [[result objectForKey:@"data"] objectForKey:@"posts"];
        for (NSDictionary *dic in arr) {
            ClassModel *model = [[ClassModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.modelArr addObject:model];
            
        }
        [self.tableView reloadData];
        
    } fail:^(NSError *error) {
        NSLog(@"%@", error);
        
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = self.IDtitle;
    [self didHeandle];
    [self tableViewSubViews];
    
}
// tableView
- (void)tableViewSubViews
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, VCWIDTH, VCHEIGH - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];
}

// tableView协议
// 区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
// 行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArr.count;
}
// heigth
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}
// cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ClassDataVewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"classDataViewCell"];
    if (!cell) {
        cell = [[ClassDataVewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"classDataViewCell"];
    }
    ClassModel *classM = [self.modelArr objectAtIndex:indexPath.row];
    cell.classM = classM;
    
    return cell;
}

// cell点击跳转方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HomeDataViewController *home = [[HomeDataViewController alloc]init];
    
    NSArray *(^block)(NSArray *) = ^(NSArray *arr)
    {
        ClassModel *class = [[ClassModel alloc]init];
        class = [self.modelArr objectAtIndex:indexPath.row];
        NSString *str1 = class.title;
        NSString *str2 = class.url;
        
        arr = [NSMutableArray arrayWithObjects:str1,str2,nil];
        
        return arr;
    };
    home.block1 = block;
    [self.navigationController pushViewController:home animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
